package v1

import (
	. "gintify/core"
	"gintify/model"
	"gintify/response"
	"github.com/gin-gonic/gin"
	"strconv"
	"strings"
)

type IBankController interface {
	IRestController
}

type BankController struct {
	// 空结构体
}

func NewBankController() *BankController {
	DB.AutoMigrate(&model.UsrBank{})
	return &BankController{}
}

func (b BankController) Post(c *gin.Context) {
	var bank = &model.UsrBank{}
	if err := c.ShouldBindJSON(bank); err != nil {
		response.Error(c, "绑定错误", nil)
		LG.Errorf("绑定出错%v\n", err)
		return
	}

	if isBankExist(bank) != nil {
		response.Fail(c, "题目已存在", nil)
		return
	}

	claims, ok := c.Get("user")
	if ok {
		// 提交者
		user := claims.(model.SysUser)
		bank.Submitter = user.Name
	} else {
		bank.Submitter = "admin"
	}

	DB.Create(&bank)
	response.Success(c, "添加新题成功", bank)
}

func (b BankController) GetList(c *gin.Context) {
	var banks []model.UsrBank
	DB.Debug().Find(&banks)
	if len(banks) == 0 {
		response.Success(c, "题库列表为空", nil)
		return
	}
	response.Success(c, "获取题库列表成功", banks)
}

func (b BankController) Get(c *gin.Context) {
	var bank model.UsrBank
	ids := c.Param("id")
	idx, err := strconv.Atoi(ids)
	if err != nil || idx < 1 {
		response.Fail(c, "URL路径有错误", err)
		return
	}

	DB.First(&bank, idx)
	if bank.ID == 0 {
		response.Fail(c, "题目不存在", nil)
		return
	}

	response.Success(c, "获取题目信息成功", bank)
}

func (b BankController) Put(c *gin.Context) {
	panic("implement me")
}

func (b BankController) Patch(c *gin.Context) {
	panic("implement me")
}

func (b BankController) Delete(c *gin.Context) {
	panic("implement me")
}

func (b BankController) Xuexi(c *gin.Context) {
	var bankQuery = &model.UsrBank{}
	if err := c.ShouldBindJSON(bankQuery); err != nil {
		response.Error(c, "绑定错误", nil)
		LG.Errorf("绑定出错%v\n", err)
		return
	}
	var result = isBankExist(bankQuery)
	if bankQuery.Answer == "" && bankQuery.Excludes == "" {
		// 仅查询
		if result != nil {
			// 查找成功
			response.Success(c, "查到此题", result)
		} else {
			// 查找失败
			response.Fail(c, "查无此题", result)
		}
	} else {
		// 更新/保存为主
		if result != nil {
			// 更新答案
			if bankQuery.Answer != "" {
				if result.Answer == "" {
					// 更新答案
					DB.Model(&result).Update("answer", bankQuery.Answer)
					response.Updated(c, "更新答案", result)
				} else {
					response.Fail(c, "题库已有答案，若需要更新，请联系管理员", result)
				}
				return
			}
			if bankQuery.Excludes != "" {
				// 旧答案有误
				if result.Answer != "" {
					LG.Debugf("旧答案有误 %s -> %s", result.Answer, "置空")
					DB.Model(&result).Update("answer", "")
				}
				// 更新排除项
				if strings.Contains(result.Excludes, bankQuery.Excludes) {
					response.Fail(c, "排除项已存在，无需重复上报", result)
				} else {
					DB.Model(&result).Update("excludes", result.Excludes+bankQuery.Excludes)
					response.Updated(c, "更新排除项", result)
				}
			}
		} else {
			// result == nil 新建
			DB.Create(&bankQuery)
			response.Created(c, "新增题目", bankQuery)
			return
		}
	}

}

func isBankExist(b *model.UsrBank) *model.UsrBank {
	var bank = &model.UsrBank{}
	var str string = b.Content
	strings.Replace(str, " ", "%", -1)
	DB.Where("content LIKE ?", str).Where(map[string]interface{}{
		"category": b.Category,
		//"confirm":  b.Confirm,
		//"content":  b.Content,
		"item_a":   b.ItemA,
		"item_b":   b.ItemB,
		"item_c":   b.ItemC,
		"item_d":   b.ItemD,
		"item_e":   b.ItemE,
		"item_f":   b.ItemF,
	}).First(bank)
	if bank.ID == 0 {
		return nil
	}
	return bank
}
